package com.CloudDream.Listener;

import com.CloudDream.Inventory.InventoryItem;
import com.CloudDream.MoonCake;
import com.CloudDream.Util.TextUtil;
import com.cryptomorin.xseries.XMaterial;
import com.handy.lib.annotation.HandyListener;
import com.handy.lib.util.BaseUtil;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.Random;

@HandyListener
public class ItemEvent implements Listener {

	private static final PotionEffectType[] effect = new PotionEffectType[]{PotionEffectType.WITHER, PotionEffectType.SLOW, PotionEffectType.WEAKNESS, PotionEffectType.HUNGER};

	@EventHandler
	public void onInventoryClick(PlayerInteractEvent e) {

		Player p = e.getPlayer();
		Inventory inv = p.getInventory();
		ItemStack itemStack = e.getItem();

		if (itemStack != null) {
			ItemMeta meta = itemStack.getItemMeta();
			if ((inv.contains(XMaterial.SKELETON_SKULL.parseMaterial())) && meta.getLore() != null) {
				if ((meta.getLocalizedName().equals("§1§2§3§4§5"))) {
					boolean isGift = (inv.contains(XMaterial.SKELETON_SKULL.parseMaterial())) &&
							(meta.getDisplayName().equals(BaseUtil.replaceChatColor("&c&l中秋七月饼礼盒")));
					if (isGift && e.getHand() == EquipmentSlot.HAND && e.getAction().name().contains("RIGHT")) {
						if (itemStack.getAmount() > 1) {
							itemStack.setAmount(itemStack.getAmount() - 1);
						} else {
							p.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
						}

						for (int i = 0; i <= 6; i++) {
							inv.addItem(InventoryItem.getMoonCake(i));
						}
						p.sendMessage(MoonCake.prefix + TextUtil.ReplaceText("Message.Open-Gift"));
						Bukkit.broadcastMessage(TextUtil.ReplacePlayer("Open-Gift-Broadcast", p.getName()));
						e.setCancelled(true);
					} else if (itemStack.equals(InventoryItem.getGift())) {
						e.setCancelled(true);
					}
				}
			}
		}
	}

	@EventHandler
	public void onPlayerConsume(PlayerItemConsumeEvent e) {

		Player p = e.getPlayer();
		ItemStack item = e.getItem();
		Inventory inv = p.getInventory();
		if (item != null) {
			if (((inv.contains(XMaterial.POTION.parseMaterial()))
					|| inv.contains(XMaterial.MILK_BUCKET.parseMaterial())
					|| inv.contains(XMaterial.COOKIE.parseMaterial())) && item.getItemMeta().getLore() == null) {
				return;

			}
			boolean isMilkBucket = (inv.contains(XMaterial.MILK_BUCKET.parseMaterial())) && (item.getItemMeta().getDisplayName().equals("§a奶粉"));
			boolean isMoonCake = (inv.contains(XMaterial.COOKIE.parseMaterial ())) &&
					((TextUtil.ReplaceItem(item, "MoonCake.lianrong.DisplayName"))
							|| (TextUtil.ReplaceItem(item, "MoonCake.wuren.DisplayName"))
							|| (TextUtil.ReplaceItem(item, "MoonCake.danhuang.DisplayName"))
							|| (TextUtil.ReplaceItem(item, "MoonCake.bingtang.DisplayName"))
							|| (TextUtil.ReplaceItem(item, "MoonCake.meigui.DisplayName"))
							|| (TextUtil.ReplaceItem(item, "MoonCake.guowei.DisplayName"))
							|| (TextUtil.ReplaceItem(item, "MoonCake.rousong.DisplayName")));
			boolean isPotion = inv.contains(XMaterial.POTION.parseMaterial()) && (
					(item.getItemMeta().getDisplayName().equals("§a枧水")) ||
							(item.getItemMeta().getDisplayName().equals("§a花生油")) ||
							(item.getItemMeta().getDisplayName().equals("§a糖浆")));
			if (p.getGameMode() != GameMode.CREATIVE) {
				if (isPotion) {
					Random rand = new Random();
					int r = rand.nextInt(effect.length - 1) + 1;
					new PotionEffect(effect[r], 1300, 1).apply(p);
					p.sendMessage(MoonCake.prefix + "§c你误喝了花生油、枧水和糖浆中的其中一样，感到了身体不适");
				}
				if (isMilkBucket) {
					p.setFoodLevel(p.getFoodLevel() - 2);
					p.setHealth(p.getHealth() - 2);
					p.sendMessage(MoonCake.prefix + "§c你误食了盐，感到了有些许口渴");
				}
			}

			if (isMoonCake) {
				if (20 - p.getFoodLevel() <= 6) {
					p.setFoodLevel(20);
				}

				if (20 - p.getFoodLevel() >= 6) {
					p.setFoodLevel(p.getFoodLevel() + 6);
				}
			}
		}
	}
}
